import {httpAction} from "@api/manage";

export const ModalFormMixin = {
    data() {
        return {
            title:"操作",
            visible:false,
            confirmLoading:false,
            model: {},
            labelCol: {
                xs: { span: 24 },
                sm: { span: 5 },
            },
            wrapperCol: {
                xs: { span: 24 },
                sm: { span: 16 },
            },
            form: this.$form.createForm(this),
        }
    },
    methods: {
        handleOk(){
            this.form.validateFields((err,values)=>{
                if(!err){
                    let formData =  this.classifyIntoFormData(values);
                    console.info("formData",formData);
                    let url = this.url.add;

                    let method = 'post';
                    if (this.model.id) {
                        url = this.url.edit
                        method = 'put'
                    }
                    console.info("url",url);
                    console.info("method",method);
                    this.confirmLoading = true;
                    httpAction(url, formData, method).then((res) => {
                        if (res.success) {
                            this.$message.success(res.message)
                            this.$emit('ok')
                            this.handleCancel()
                        } else {
                            this.$message.warning(res.message)
                        }
                    }).finally(() => {
                        this.confirmLoading = false
                    });
                }
            })
        },

        handleCancel(){
            this.visible=false;
        }
    }
}